// pages/volunteer/ranking.js
Page({
  data: {
    activeTab: 'monthly',
    rankingList: [
      { id: 1, rank: 1, name: '青苔', points: 2333 },
      { id: 2, rank: 2, name: '阳光', points: 2100 },
      { id: 3, rank: 3, name: '爱心', points: 1980 },
      { id: 4, rank: 4, name: '希望', points: 1850 },
      { id: 5, rank: 5, name: '温暖', points: 1720 },
      { id: 6, rank: 6, name: '奉献', points: 1650 },
      { id: 7, rank: 7, name: '关怀', points: 1580 },
      { id: 8, rank: 8, name: '守护', points: 1500 },
      { id: 9, rank: 9, name: '陪伴', points: 1420 }
    ],
    userRank: {
      rank: 10,
      name: '晴天',
      points: 1333
    }
  },

  onLoad: function (options) {
    console.log('排行榜页面加载');
    this.loadRankingData();
  },

  onShow: function () {
    // 页面显示时刷新数据
    this.refreshData();
  },

  // 加载排行榜数据
  loadRankingData: function () {
    // 从本地存储或服务器获取排行榜数据
    const rankingData = wx.getStorageSync('rankingData');
    if (rankingData) {
      this.setData({
        rankingList: rankingData.rankingList || this.data.rankingList,
        userRank: rankingData.userRank || this.data.userRank
      });
    }
  },

  // 刷新数据
  refreshData: function () {
    // 刷新排行榜数据
    this.loadRankingData();
  },

  // 切换标签页
  switchTab: function (e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
    
    // 根据标签加载对应的排行榜数据
    this.loadTabData(tab);
  },

  // 加载标签页数据
  loadTabData: function (tab) {
    // 这里可以根据不同的标签加载不同的排行榜数据
    console.log('切换到', tab, '排行榜');
    
    // 模拟加载不同时间段的数据
    let rankingList = [];
    let userRank = {};
    
    switch (tab) {
      case 'weekly':
        rankingList = [
          { id: 1, rank: 1, name: '青苔', points: 588 },
          { id: 2, rank: 2, name: '阳光', points: 520 },
          { id: 3, rank: 3, name: '爱心', points: 480 },
          { id: 4, rank: 4, name: '希望', points: 450 },
          { id: 5, rank: 5, name: '温暖', points: 420 }
        ];
        userRank = { rank: 8, name: '晴天', points: 333 };
        break;
      case 'monthly':
        rankingList = [
          { id: 1, rank: 1, name: '青苔', points: 2333 },
          { id: 2, rank: 2, name: '阳光', points: 2100 },
          { id: 3, rank: 3, name: '爱心', points: 1980 },
          { id: 4, rank: 4, name: '希望', points: 1850 },
          { id: 5, rank: 5, name: '温暖', points: 1720 },
          { id: 6, rank: 6, name: '奉献', points: 1650 },
          { id: 7, rank: 7, name: '关怀', points: 1580 },
          { id: 8, rank: 8, name: '守护', points: 1500 },
          { id: 9, rank: 9, name: '陪伴', points: 1420 }
        ];
        userRank = { rank: 10, name: '晴天', points: 1333 };
        break;
      case 'yearly':
        rankingList = [
          { id: 1, rank: 1, name: '青苔', points: 15888 },
          { id: 2, rank: 2, name: '阳光', points: 15200 },
          { id: 3, rank: 3, name: '爱心', points: 14800 },
          { id: 4, rank: 4, name: '希望', points: 14200 },
          { id: 5, rank: 5, name: '温暖', points: 13800 }
        ];
        userRank = { rank: 15, name: '晴天', points: 12333 };
        break;
    }
    
    this.setData({
      rankingList,
      userRank
    });
  },

  // 跳转到我的奖项
  goToAwards: function () {
    wx.navigateTo({
      url: '/pages/volunteer/awards'
    });
  },



  // 分享功能
  onShareAppMessage: function () {
    return {
      title: '志愿者排行榜',
      path: '/pages/volunteer/ranking',
      imageUrl: '/images/share-ranking.png'
    };
  },

  // 分享到朋友圈
  onShareTimeline: function () {
    return {
      title: '志愿者排行榜',
      imageUrl: '/images/share-ranking.png'
    };
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});
